﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Access.PowerTools.Utilities
{
    public sealed class Strings
    {
        public static string EncodeObjectName(string objectName)
        {
            string encodedObjectName = "";
            for (int i = 0; i < objectName.Length; i++)
            {
                if (
                    Char.IsLetterOrDigit(objectName[i]) ||
                    objectName[i] == '-' ||
                    objectName[i] == '_' ||
                    objectName[i] == ' '
                    )
                    encodedObjectName += objectName[i];
                else
                    //encodedObjectName += string.Format("%{0:X2}", (UInt16)objectName[i]);
                    encodedObjectName += string.Format("_x{0:X4}_", (UInt16)objectName[i]);
            }
            return encodedObjectName;
        }

        public static string DecodeObjectName(string objectName)
        {
            string decodedObjectName = "";
            for (int i = 0; i < objectName.Length; i++)
            {
                if (objectName[i] == '_' && 
                    i < objectName.Length-1 &&
                    objectName[i+1] == 'x' )
                {
                    string x = objectName.Substring(i + 2, 4);
                    char c = (Char)UInt32.Parse(x, System.Globalization.NumberStyles.AllowHexSpecifier);
                    decodedObjectName += c;
                    i += 6;
                }
                else
                    decodedObjectName += objectName[i];
            }
            return decodedObjectName;
        }
    }
}
